package com.monlyu.tag.extend;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.math.RandomUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.monlyu.tag.BaseTag;
import com.monlyu.util.FtlUtil;
import com.monlyu.util.LogUtil;
import com.monlyu.util.StringUtil;

/********************************************************************************
 * 
 * <b>功能描述☞</b>Html富文本编辑标签<br>
 * 
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.tag.extend.CKEditorTag.java<br>
 * <b>开发日期☞</b>2010-2-23 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.2
 * 
 ********************************************************************************/
public class KEditorTag extends BaseTag {

	private String name;
	private String value;
	private String skin;
	private String width;
	private String height;
	private String toolbar;
	private String id;
	private int resizeMode;

	public void setResizeMode(int resizeMode) {
		this.resizeMode = resizeMode;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setSkin(String skin) {
		this.skin = skin;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public void setToolbar(String toolbar) {
		this.toolbar = toolbar;
	}

	public void setValue(String value) {

		this.value = value;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int doStartTag() {
		try {
			cheakId();
			write2Page(FtlUtil.render2Ftl(buildData(), "KEditor.ftl"));
		} catch (Exception e) {
			LogUtil.log(getClass()).error("解析CKEditor标签错误");
		} finally {
			name = width = value = height = id = toolbar = skin = null;
		}
		return SKIP_BODY;
	}

	private void cheakId() {
		if (StringUtil.isEmpty(id)) {
			if (StringUtil.isEmpty(name)) {
				setId("ke_rno" + RandomUtils.nextInt(404));
			} else {
				setId("ke_" + name.replace(".", ""));
			}
		}
		if (StringUtil.isEmpty(height)) {
			setHeight("400");
		}
		if (StringUtil.isEmpty(width)) {
			setWidth("100%");
		}
	}

	/**
	 * 添加数据
	 * 
	 * @since 2010-2-23
	 */
	private Map<String, Object> buildData() {
		Map<String, Object> map = Maps.newHashMap();
		map.put("script", COMMON_WEBDIR);
		map.put("id", id);
		if (!StringUtil.isTrimEmpty(name)) {
			map.put("name", name);
		}
		if (StringUtil.isEmpty(value)) {
			String stack = (String) getValueFromValueStack(name);
			if (stack != null) {
				map.put("value", stack);
			}
		} else {
			map.put("value", value);
		}
		map.put("agrs", setArgs());
		return map;
	}

	/**
	 * 设置参数
	 * 
	 * @since 2010-2-23
	 */
	private String setArgs() {
		List<String> list = Lists.newArrayList();
		list.add("id:'" + id + "'");
		list.add("resizeMode:" + resizeMode);
		if (!StringUtil.isTrimEmpty(skin)) {
			list.add("skinType:'" + skin + "'");
		}
		if (!StringUtil.isTrimEmpty(height)) {
			if (height.endsWith("%")) {
				list.add("height:'" + height + "'");
			} else {
				list.add("height:'" + height + "px'");
			}
		}
		if (!StringUtil.isTrimEmpty(width)) {
			if (width.endsWith("%")) {
				list.add("width:'" + width + "'");
			} else {
				list.add("width:'" + width + "px'");
			}
		}
		if (!StringUtil.isTrimEmpty(toolbar)) {
			list.add("toolbar:'" + toolbar + "'");
		}
		return "{" + list.toString().substring(1, list.toString().length() - 1) + "}";
	}
}
